<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => reset()">
      <a-form ref="formRef" class="form-style-flex" :model="formState">
        <a-form-item name="zjm" class="ml-2">
          <a-input v-model:value="formState.zjm" class="!w-72" placeholder="药品编号/药品名称/助记码" allow-clear />
        </a-form-item>
        <a-form-item name="dateRange" label="发药日期" class="ml-3 mr-3">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="dateRangePrice" label="收费日期" class="ml-3 mr-3">
          <a-range-picker v-model:value="formState.dateRangePrice" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="operatio" label="" class="ml-3 mr-3">
          <a-radio-group v-model:value="formState.operatio" @change="getData">
            <a-radio value="1">全部</a-radio>
            <a-radio value="2">门诊</a-radio>
            <a-radio value="3">住院</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item name="sfhb" label="" class="ml-3 mr-3">
          <a-checkbox v-model:checked="formState.sfhb" @change="getData">合并</a-checkbox>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="mb-3 flex justify-end">
        <BaseExport
          type="药库"
          name="药品销售"
          code="000405"
          :search-params="{
            stdate: formState.dateRange?.[0],
            eddate: formState.dateRange?.[1],
            tollStdate: formState.dateRangePrice?.[0],
            tollEddate: formState.dateRangePrice?.[1],
            zjm: formState.zjm,
            operationmode: formatOperateMode(),
            sfhb: formState.sfhb ? 1 : 0,
            lb: formatLb()
          }"
          >导出</BaseExport
        >
      </div>
      <!-- 000405   000406 -->
      <BaseTable :loading="loading" :column-code="formState.sfhb ? '000406' : '000405'" :data-source="tableData" bordered fill :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            <div>{{ index + 1 }}</div>
          </template>
          <template v-if="column.dataIndex === 'ypmc'">
            <MedicineInfo :value="{ mc: record.ypmc, gg: record.gg, cj: record.cj }" />
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <a-table-summary-cell />
              <a-table-summary-cell>
                <a-typography-text>合计</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell />
              <a-table-summary-cell />
              <a-table-summary-cell />
              <a-table-summary-cell />
              <a-table-summary-cell />
              <a-table-summary-cell v-if="formState.sfhb" />
              <a-table-summary-cell class="text-right">
                <a-typography-text>{{ formatAmount(countData.slhj) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell class="text-right">
                <a-typography-text>{{ formatAmount(countData.xsjehj) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell class="text-right">
                <a-typography-text>{{ formatAmount(countData.xscbhj) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell class="text-right">
                <a-typography-text>{{ formatAmount(countData.xsmlhj) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell />
              <a-table-summary-cell v-if="!formState.sfhb" />
              <a-table-summary-cell v-if="!formState.sfhb" />
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { formatAmount } from '@/utils/format.js'

const formRef = ref(null)
const countData = ref([])
const formState = ref({
  zjm: '',
  operatio: '1',
  sfhb: false,
  dateRange: [],
  dateRangePrice: [dayjs(new Date().setDate(new Date().getDate() - 20)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const { loading, tableData, getData, pagination } = useTable({
  url: '/drugStorage/psiform/getDrugSalesList',
  immediate: true,
  formatParams: () => {
    return {
      stdate: formState.value.dateRange?.[0],
      eddate: formState.value.dateRange?.[1],
      tollStdate: formState.value.dateRangePrice?.[0],
      tollEddate: formState.value.dateRangePrice?.[1],
      zjm: formState.value.zjm,
      operationmode: formatOperateMode(),
      sfhb: formState.value.sfhb ? 1 : 0,
      lb: formatLb()
    }
  },
  onSuccess: () => {
    getCountData()
  }
})
const getCountData = async () => {
  const { data, code } = await post('/drugStorage/psiform/getDrugSalesListHJ', {
    stdate: formState.value.dateRange?.[0],
    eddate: formState.value.dateRange?.[1],
    tollStdate: formState.value.dateRangePrice?.[0],
    tollEddate: formState.value.dateRangePrice?.[1],
    zjm: formState.value.zjm,
    operationmode: formatOperateMode(),
    sfhb: formState.value.sfhb ? 1 : 0,
    lb: formatLb()
  })
  if (code === 0) {
    countData.value = data
  }
}
const formatOperateMode = () => {
  if (formState.value.operatio === '2') {
    return ['4', '5']
  } else if (formState.value.operatio === '3') {
    return ['2', '6']
  } else {
    return ['4', '5', '2', '6']
  }
}
const formatLb = () => {
  if (formState.value.operatio === '2') {
    return '2'
  } else if (formState.value.operatio === '3') {
    return '6'
  } else {
    return ''
  }
}
const reset = () => {
  formState.value = {
    zjm: '',
    dateRange: [dayjs(new Date().setDate(new Date().getDate() - 20)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
  }
  getData()
}
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
